/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.operativa;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.context.FacesContext;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.operativa.exception.CharlaPProgException;
import co.gov.medellin.sdep.sisdep.operativa.exception.CharlaPProgRuntimeException;
import co.gov.medellin.sdep.sisdep.operativa.service.ICharlaPAgendaService;
import co.gov.medellin.sdep.sisdep.operativa.service.ICharlaPProgService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.model.CharlaPProg;

/**
 * @author Usuario
 * 
 */
public class CharlaPProgController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/** bean para el manejo del formulario de busqueda CharlaPProg */
	private CharlaPProg charlaPProgSearch = new CharlaPProg();

	/** Objeto contenedor del listado de CharlaPProg luego de una consulta. */
	private transient DataModel charlaPProgListModel;

	/** bean para el manejo del formulario de creacion/edicion. CharlaPProg */
	private CharlaPProg charlaPProg = new CharlaPProg();

	/** instancia de servicio charlaPProgService */
	private ICharlaPProgService charlaPProgService;
	
	/** instancia de servicio charlaPAgendaService */
	private ICharlaPAgendaService charlaPAgendaService;
	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public CharlaPProgController() {
		this.charlaPProgListModel = new ListDataModel();
	}
	
	CharlaPAgendaController charlaPAgendaController ;
	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchCharlaPProgByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getCharlaPProgService().load(1, 10,
					this.charlaPProgSearch);
			this.charlaPProgListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.charlaPProgListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.charlaPProgListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.CHARLA_P_PROG_SEARCH_VIEW;
	}
	
	/**
	 * 
	 * @return String
	 */
	public String searchCharlaPProgByTipoCharla() {
		String msg = "";
		try {
			List valores = null;
			valores = getCharlaPProgService().loadByTipoCharla(1, 10,
					this.charlaPProgSearch);
			this.charlaPProgListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.charlaPProgListModel.getWrappedData() == null) {
			msg = AppConstants.NO_RECORDS_FOUND;
		} else {
			if (((List) this.charlaPProgListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.CHARLA_P_PROG_SEARCH_VIEW;
	}	

  /**
	 * Metodo que permite espeficicar la funcionalidad para la gestion [Crear / Actualizar] de un elmento tipo:
	 * CharlaPProg.
	 *     
	 * @author programador.java@consult-soft.com  05-06-2012
	 * @return String Vista a la cual debe retornar
	 * 	 
	 */
	public String manage() {
		String returnView = AppConstants.CHARLA_P_PROG_SEARCH_VIEW;
		if(this.charlaPProg.getId() == null) {
       returnView = create();
		} else {
       returnView = update();
		}
		return returnView;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.charlaPProg.getId() == null) {	
					charlaPProg.setIdLugar(new Long(charlaPProg.getIdLugarStr()));
					charlaPProg.setIdTipoCharla(new Long(charlaPProg.getIdTipoCharlaStr()));
					charlaPProg.setDuracion(new Long(charlaPProg.getDuracionStr()));
					charlaPProg.setUnidadDuracion(new Long(charlaPProg.getUnidadDuracionStr()));
					getCharlaPProgService().insert(this.charlaPProg);
					msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (CharlaPProgException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (CharlaPProgRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- charlaPProg " + msg, null, null);
		return AppConstants.CHARLA_P_PROG_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.charlaPProg.getId() != null) {
					charlaPProg.setIdLugar(new Long(charlaPProg.getIdLugarStr()));
					charlaPProg.setIdTipoCharla(new Long(charlaPProg.getIdTipoCharlaStr()));
					charlaPProg.setDuracion(new Long(charlaPProg.getDuracionStr()));
					charlaPProg.setUnidadDuracion(new Long(charlaPProg.getUnidadDuracionStr()));
					getCharlaPProgService().update(this.charlaPProg);
					msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (CharlaPProgException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (CharlaPProgRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		((MessageModalWindowController) getBean(AppConstants.MESSAGE_CONTROLLER_BEAN))
				.setSuccessMessage("- Valor Tipo Maestra " + msg, null, null);
		return AppConstants.CHARLA_P_PROG_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createCharlaPProg() {
		charlaPProg = new CharlaPProg();
		charlaPProg.setId(null);
		charlaPProg.setUsuarioCrea(getLoggedUserID());
		charlaPProg.setUsuarioModifica(getLoggedUserID());
		//charlaPProg.setEstado("ACTIVO");
		return AppConstants.CHARLA_P_PROG_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateCharlaPProg() {
		Map params = getExternalContext().getRequestParameterMap();
		String charlaPProgDataIndex = (String) params
				.get("charlaPProgDataIndex");
		this.charlaPProg = (CharlaPProg) ((List) this.charlaPProgListModel
				.getWrappedData())
				.get(Integer.parseInt(charlaPProgDataIndex));
		return AppConstants.CHARLA_P_PROG_UPDATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String asistenciaCharlaPProg() {
		Map params = getExternalContext().getRequestParameterMap();
		String charlaPProgDataIndex = (String) params
				.get("charlaPProgDataIndex");
		this.charlaPProg = (CharlaPProg) ((List) this.charlaPProgListModel
				.getWrappedData())
				.get(Integer.parseInt(charlaPProgDataIndex));

		getCharlaPAgendaController().charlaPAgendaSearch.setIdCharlaPProg(charlaPProg.getId());
		getCharlaPAgendaController().charlaPAgendaSearch.setEstado(charlaPProg.getEstado());
		getCharlaPAgendaController().searchCharlaPAgendaByIdCharla(charlaPProg.getId(), charlaPAgendaService);
		
		return AppConstants.CHARLA_P_AGENDA_SEARCH_VIEW;
	}
	
	/**
	 * 
	 * @return
	 */
	public String cancelCharlaPProg() {
		return AppConstants.CHARLA_P_PROG_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public CharlaPProg getCharlaPProgSearch() {
		return charlaPProgSearch;
	}

	/**
	 * 
	 * @param charlaPProgSearch
	 */
	public void setcharlaPProgSearch(CharlaPProg charlaPProgSearch) {
		this.charlaPProgSearch = charlaPProgSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getcharlaPProgListModel() {
		return charlaPProgListModel;
	}

	/**
	 * 
	 * @param charlaPProgListModel
	 */
	public void setcharlaPProgListModel(DataModel charlaPProgListModel) {
		this.charlaPProgListModel = charlaPProgListModel;
	}

	/**
	 * 
	 * @return
	 */
	public CharlaPProg getCharlaPProg() {
		return charlaPProg;
	}

	/**
	 * 
	 * @param charlaPProg
	 */
	public void setcharlaPProg(CharlaPProg charlaPProg) {
		this.charlaPProg = charlaPProg;
	}

	/**
	 * obtencion del servicio charlaPProgService
	 * 
	 * @return
	 */
	public ICharlaPProgService getCharlaPProgService() {
		return charlaPProgService;
	}

	/**
	 * establecimiento del servicio charlaPProgService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param charlaPProgService
	 */
	public void setCharlaPProgService(
			ICharlaPProgService charlaPProgService) {
		this.charlaPProgService = charlaPProgService;
	}

	public ICharlaPAgendaService getCharlaPAgendaService() {
		return charlaPAgendaService;
	}

	public void setCharlaPAgendaService(ICharlaPAgendaService charlaPAgendaService) {
		this.charlaPAgendaService = charlaPAgendaService;
	}

	public CharlaPAgendaController getCharlaPAgendaController() {
		return charlaPAgendaController;
	}

	public void setCharlaPAgendaController(
			CharlaPAgendaController charlaPAgendaController) {
		this.charlaPAgendaController = charlaPAgendaController;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
